package com.haochang.chunkvod.view.vod.dialog;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.controller.activity.cash.CheckOutActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.tools.vod.log.Logger;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;
import com.haochang.chunkvod.tools.vod.other.DrawableUtils;
import com.haochang.chunkvod.view.vod.activity.SingleInputActivity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
 * @author qintao
 *         created at 2016/7/27 11:20
 *         单选dialog，右边显示红色勾
 *         里面有两类监听器
 */

public class SingleSelectionDialog extends Dialog
{
	public SingleSelectionDialog(Context context)
	{
		super(context);
	}

	public SingleSelectionDialog(Context context, int theme)
	{
		super(context, theme);
	}

	protected SingleSelectionDialog(Context context, boolean cancelable, OnCancelListener cancelListener)
	{
		super(context, cancelable, cancelListener);
	}

	public static class Builder
	{
		private DialogEnum dialogEnum = DialogEnum.DEFAULT;
		private Context context;
		private CharSequence title; //对话框标题
		private List<DialogItemInfo> dataList = new ArrayList<DialogItemInfo>();
		private OnSelectListener mOnSelectListener; //对话框点击事件
		private OnSingleItemChoosedListener mOnSingleItemChoosedListener; //对话框点击事件 返回item
		private QuickAdapter quickAdapter = null;

		public Builder(Context context)
		{
			this.context = context;
		}

		public Builder setTitle(int title)
		{
			this.title = (String) context.getText(title);
			return this;
		}

		public Builder setTitle(CharSequence title)
		{
			this.title = title;
			return this;
		}

		public Builder setDataList(List<DialogItemInfo> dataList)
		{
			this.dataList = dataList;
			return this;
		}

		public Builder dialogEnum(DialogEnum dialogEnum)
		{
			this.dialogEnum = dialogEnum;
			return this;
		}

		public List<DialogItemInfo> getDataList()
		{
			return dataList;
		}

		//设置单选List选项及事件，这些属性在之后的createDialog中用到，这里使用Android系统创建dialog的风格
		public Builder setSingleChoiceItems(List<DialogItemInfo> dataList, final OnSelectListener listener,
				final OnSingleItemChoosedListener mOnSingleItemChoosedListener)
		{
			this.dataList.clear();
			this.dataList.addAll(dataList);
			if (null != listener)
			{
				this.mOnSelectListener = listener;
			}
			if (null != mOnSingleItemChoosedListener)
			{
				this.mOnSingleItemChoosedListener = mOnSingleItemChoosedListener;
			}
			return this;
		}

		/**
		 * 默认调用这个
		 * @param dataList
		 * @param listener
		 * @return
		 */
		public Builder setSingleChoiceItems(List<DialogItemInfo> dataList, final OnSelectListener listener)
		{
			return setSingleChoiceItems(dataList, listener, null);
		}

		/**
		 * 这个是单选后返回整个item
		 * @param dataList
		 * @param mOnSingleItemChoosedListener
		 * @return
		 */
		public Builder setSingleChoiceItems(List<DialogItemInfo> dataList, OnSingleItemChoosedListener mOnSingleItemChoosedListener)
		{
			return setSingleChoiceItems(dataList, null, mOnSingleItemChoosedListener);
		}

		public SingleSelectionDialog createDialog()
		{
			return createDialog(0);
		}

		/**
		 * 根据规定的最大条数显示列表
		 *
		 * @param rows 最大显示条数
		 * @return
		 */
		public SingleSelectionDialog createDialog(int rows)
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final SingleSelectionDialog dialog = new SingleSelectionDialog(context, R.style.Alert_Dialog);
			LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.single_selection_dialog, null, false);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			BaseListView listView = (BaseListView) layout.findViewById(R.id.listView);
			if (rows == 0)
			{
				listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, DipPxConversion.dipToPx(context, 40 * dataList.size())));
			} else
			{
				//当数据条数小于设置最大显示条数时，高度设置为实际高度，当大于设置条数时，显示设置条数的高度
				listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
						dataList.size() <= rows ? DipPxConversion.dipToPx(context, 40 * dataList.size()) : DipPxConversion.dipToPx(context, 40 * rows)));
			}
			quickAdapter = new QuickAdapter<DialogItemInfo>(context, R.layout.single_selection_list_item, dataList)
			{
				@Override
				protected void convert(final BaseAdapterHelper helper, final DialogItemInfo item)
				{
					/**
					 * 如果是支付类型，带有图标
					 */
					helper.setText(R.id.tv_name, item.getName());
					if (dialogEnum == DialogEnum.PAY)
					{
						BaseTextView tv_name = helper.getView(R.id.tv_name);
						if (item.getName().equals("支付宝"))
						{
							tv_name.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.zhifubao), null, null, null);
						} else if (item.getName().equals("微信"))
						{
							tv_name.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.weixin), null, null, null);
						} else if (item.getName().equals("刷卡"))
							tv_name.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.shuaka), null, null, null);
					}
					if (item.isSelected())
					{
						helper.getView(R.id.img_gou).setVisibility(View.VISIBLE);
						helper.setTextColorRes(R.id.tv_name, R.color.color_f04e39);
					} else
					{
						helper.getView(R.id.img_gou).setVisibility(View.GONE);
						helper.setTextColorRes(R.id.tv_name, R.color.color_737373);
					}
					helper.getView().setOnClickListener(new OnBaseClickListener()
					{
						@Override
						public void onBaseClick(View v)
						{
							if (dialogEnum == DialogEnum.SPECIAL_PAY)
							{
								if (!item.getCode().equals("normal"))
								{
									Intent intent = new Intent(context, SingleInputActivity.class);
									intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT_HINT, "请输入密码");
									intent.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, "特殊收款");
									intent.putExtra(IntentKey.BTN_SURE, "确定");
									intent.putExtra(IntentKey.BTN_CANCEL, "取消");
									intent.putExtra(IntentKey.DIALOGITEMINFO, (Parcelable) item);
									intent.putExtra(IntentKey.ACTIVITY_ENUM, SingleInputActivity.ActivityEnum.SPECIAL_PERMISSION);
									((CheckOutActivity) context).startActivityForResult(intent, IntentCode.SPECIAL_PAY_REQUEST);
								} else
								{
									onRefresh(item);
									if (mOnSelectListener != null)
									{
										mOnSelectListener.getValue(item.getCode(), item.getName());
									}
									if (mOnSingleItemChoosedListener != null)
									{
										mOnSingleItemChoosedListener.getSelectedItem(item);
									}
								}
								dialog.dismiss();
							} else
							{
								onRefresh(item);
								if (mOnSelectListener != null)
								{
									mOnSelectListener.getValue(item.getCode(), item.getName());
								}
								if (mOnSingleItemChoosedListener != null)
								{
									mOnSingleItemChoosedListener.getSelectedItem(item);
								}
								dialog.dismiss();
							}
						}
					});
				}
			};
			listView.setAdapter(quickAdapter);
			BaseTextView tv_title = (BaseTextView) layout.findViewById(R.id.tv_title);
			tv_title.setText(title);
			return dialog;
		}

		/**
		 * 刷新数据
		 *
		 * @param info
		 */
		private void onRefresh(DialogItemInfo info)
		{
			for (DialogItemInfo itemInfo : dataList)
			{
				if (itemInfo.getName().equals(info.getName()))
				{
					itemInfo.setSelected(true);
				} else
				{
					itemInfo.setSelected(false);
				}
			}
		}

	}

	public interface OnSelectListener
	{
		public void getValue(String code, String name);
	}

	public interface OnSingleItemChoosedListener
	{
		void getSelectedItem(DialogItemInfo itemInfo);
	}

	public enum DialogEnum
	{
		/**
		 * 默认类型，无图标
		 */
		DEFAULT,
		/**
		 * 支付类型，带图标
		 */
		PAY,
		/**
		 * 特殊收款
		 */
		SPECIAL_PAY;
	}
}
